package CommonEvent;


import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.touch.offset.PointOption;
import org.openqa.selenium.Dimension;
import static Utils.MyLogger.logInfo;
/**
 * @author 锄禾
 * @projectName MkPack
 * @description: TODO
 */
public class MonkeySwipEvent extends MonkeyEvent {
    private Dimension dimension;
    private TouchAction touchAction;
    private AndroidDriver androidDriver;
    private long sleepTime = 300;
    private int MaxScroll = 1;

    public MonkeySwipEvent(AndroidDriver driver){
        super(MonkeyEvent.EVENT_TYPE_SWIPE);
        androidDriver = driver;
    }

    @Override
    public int injectEvent(){
        return 0;
    }

    /**
    　* @author 锄禾
    　* @description: 真实的向上滑动事件
    　* @param
    　* @return
    　*/
    public int injectEventScrollUP(){
        try {
            logInfo("向上滑动操作开始！");
            this.scrollUp(MaxScroll);
            logInfo("向上滑动操作结束");
            return MonkeyEvent.INJECT_SUCCESS;
        }catch (Exception E){
            logInfo(this.getClass().getName() + "-" + "注入事件异常：" + E);
            return INJECT_FIAL;
        }
    }

    /**
    　* @author 锄禾
    　* @description: 真实的向下滚动事件
    　* @param
    　* @return
    　*/
    public int injectEventScrollDown(){
        try {
            this.scrollDown(MaxScroll);
            logInfo("向下滚动事件完成！");
            return MonkeyEvent.INJECT_SUCCESS;
        }catch (Exception E){
            logInfo(this.getClass().getName() + "-" + "向下滑动事件异常：" + E);
            return MonkeyEvent.INJECT_FIAL;
        }
    }

    /**
    　* @author 锄禾
    　* @description: 向上滑动操作
    　* @param loop 次数
    　* @return
    　*/
    public void scrollUp(int loop) throws InterruptedException {
        for (int i = 0; i < loop; i++) {
            touchAction = new TouchAction(androidDriver);
            touchAction.press(PointOption.point((int)(getWindowSize().width * 0.5),
                    (int)(getWindowSize().height * 0.3)));
            touchAction.moveTo(PointOption.point((int)(getWindowSize().width * 0.5),
                    (int)(getWindowSize().height * 0.8)));
            touchAction.release();
            touchAction.perform();
            logInfo("向上滑动！");
            Thread.sleep(sleepTime);
        }
    }

    /**
    　* @author 锄禾
    　* @description: 向下滑动操作
    　* @param loop 次数
    　* @return
    　*/
    public void scrollDown(int loop) throws InterruptedException {
        for (int i = 0; i < loop; i++) {
            touchAction = new TouchAction(androidDriver);
            touchAction.press(PointOption.point((int)(getWindowSize().width * 0.5),
                    (int)(getWindowSize().height * 0.8)));
            touchAction.moveTo(PointOption.point((int)(getWindowSize().width * 0.5),
                    (int)(getWindowSize().height * 0.3)));
            touchAction.release();
            touchAction.perform();
            logInfo("向下滑动！");
            Thread.sleep(sleepTime);
        }
    }

    /**
    　* @author 锄禾
    　* @description: 获取当前界面尺寸
    　* @param
    　* @return
    　*/
    public Dimension getWindowSize(){
        dimension = androidDriver.manage().window().getSize();
        return dimension;
    }

}
